/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bean;

import com.entities.EventLibrary;
import com.entities.Messenger;
import com.model.NotificationModel;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Tuan Long
 */
@Named(value = "notificationManager")
@RequestScoped
public class NotificationManager {

    /**
     * Creates a new instance of NotificationManager
     */
    public NotificationManager() {
    }
    NotificationModel model = new NotificationModel();
    private int msgId;

    public int getMsgId() {
        return msgId;
    }

    public void setMsgId(int msgId) {
        this.msgId = msgId;
    }

    private int countday;

    public int getCountday() {
        return countday;
    }

    public void setCountday(int countday) {
        this.countday = countday;
    }

    private List<Messenger> messages;

    public List<Messenger> getMessages() {
        getListMessages();
        return messages;
    }

    private void getListMessages() {
        messages = model.getNewMessage();
        for (Messenger messenger : messages) {
            String content = messenger.getContent();
            if (content.length() > 50) {
                String shortcontent = content.substring(0, 20) + "...";
                messenger.setContent(shortcontent);
            }            
        }
    }

    private List<Messenger> chatbox;

    public List<Messenger> getChatbox() {
        activeMessageRead();
        return chatbox;
    }

    private void activeMessageRead() {
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if (req.getParameter("msgId") != null) {
            String id = req.getParameter("msgId");
            try {
                msgId = Integer.parseInt(id);
                chatbox = model.activeMessageRead(msgId);
            } catch (NumberFormatException ex) {
                msgId = 0;
            }
        }
    }
    
    private int newRegis;
    private int newComments;

    public int getNewRegis() {
        newRegis = model.getNewRegis();
        return newRegis;
    }

    public int getNewComments() {
        newComments = model.getNewComments();
        return newComments;
    }
    
    

}
